package com.blog.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blog.service.entity.Article;
import com.blog.service.inter.ArticleService;
import com.blog.service.model.ArticleMapper;
import com.blog.web.page.Page;
import com.blog.web.page.WebPage;
import com.github.pagehelper.PageHelper;

/**
 * 文件名： com.blog.service.impl.ArticleServiceImpl.java
 * 作者： WenBin
 * 日期： 2017年4月12日
 * 功能说明： 日志服务类
 * =========================================================
 * 修改记录：
 * 修改作者 日期 修改内容
 * =========================================================
 * Copyright (c) 2010-2011 .All rights reserved.
 */
@Service
public class ArticleServiceImpl implements ArticleService {

	@Autowired
	private ArticleMapper articleMapper;

	@Override
	public Article saveArticle(Article article) {

		int i = articleMapper.insertSelective(article);
		if (i > 0) {
			return article;
		}
		return null;
	}

	@Override
	public Article updateArticle(Article article) {

		int i = articleMapper.updateByPrimaryKey(article);
		if (i > 0) {
			return article;
		}
		return null;
	}

	@Override
	public Article deleteArticle(Article article) {

		int i = articleMapper.deleteByPrimaryKey(article.getArticleId());
		if (i > 0) {
			return article;
		}
		return null;
	}

	@Override
	public Page queryListByPage(WebPage page) {

		Map<String, Object> params = page.getParams();
		PageHelper.startPage(page.getPageNumber(), page.getLimit() == null ? 10 : page.getLimit());
		List<Map<String, Object>> l = articleMapper.queryListByParams(params);
		com.github.pagehelper.Page<Map<String, Object>>  pg = (com.github.pagehelper.Page<Map<String, Object>>)l;
		Page rtpage = new Page();
		rtpage.setRows(l);
		rtpage.setTotal(pg.getTotal());
		return rtpage;
	}

	@Override
	public Article findArticleById(String article_id) {

		return articleMapper.selectByPrimaryKey(article_id);
	}

	@Override
	public Page queryAllListByPage(WebPage page) {

		Map<String, Object> params = page.getParams();
		PageHelper.startPage(page.getPageNumber(), page.getLimit());
		List<Map<String, Object>> l = articleMapper.queryAllListByParams(params);
		com.github.pagehelper.Page<Map<String, Object>>  pg = (com.github.pagehelper.Page<Map<String, Object>>)l;
		Page rtpage = new Page();
		rtpage.setRows(l);
		rtpage.setTotal(pg.getTotal());
		return rtpage;
	}

}
